#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear
cd /root/
function check_ip(){
IP=`wget http://members.3322.org/dyndns/getip -O - -q; echo`
    if [[ "$IP" == '' ]]; then
		echo '无法检测您的IP,可能会影响到您接下来的搭建工作';
		read -p '请输入您的公网IP:' IP;
		[[ "$IP" == '' ]] && InputIPAddress;
	fi;
	[[ "$IP" != '' ]] && echo -e '\033[32m[OK]\033[0m 您的IP是:' && echo $IP;	
	sleep 2
}

#检查操作系统
function system(){
    if [ -f /etc/redhat-release ];then
        OS=centos
    elif [ ! -z "`cat /etc/issue | grep bian`" ];then
        OS=debian
    elif [ ! -z "`cat /etc/issue | grep Ubuntu`" ];then
        OS=ubuntu
    else
        echo -e "\e[1;31m\n非常抱歉,不支持的操作系统!3秒后将返回菜单!\e[0m"
        sleep 3
		memu
    fi
	echo 操作系统: $OS
	sleep 3
}   
#判定系统位数
function set32_64bit(){
    if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LONG_BIT) = '64' ] ; then
        echo "64位系统!"
		sleep 3
		set64
    else
	    echo "32位系统!"
		sleep 3
        set32

    fi

}
#安装系统环境
function install_dep(){
    if [ $OS = centos ]; then
        yum update -y
        yum install gcc gcc-c++ make unzip zip expect tar java openssl -y
    else
        apt-get update -y
        apt-get install gcc make expect unzip zip curl -y
		apt-get install default-jre openssl -y
    fi


}
zewin=https://
function set32(){
rm -rf vpnserver*
wget http://git.oschina.net/lusao/svpen/raw/master/softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-x86-32bit.tar.gz
tar -zxvf softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-x86-32bit.tar.gz
rm -f softether-vpnserve*
}


function set64(){
rm -rf vpnserver*
wget http://git.oschina.net/lusao/svpen/raw/master/softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-x64-64bit.tar.gz
tar -zxvf softether-vpnserver-v4.21-9613-beta-2016.04.24-linux-x64-64bit.tar.gz
rm -f softether-vpnserve*
}

function get_ready(){
echo -e "感谢使用一键Sevpn脚本,我是\033[40;31m LuSao \033[0m!"
read -p "(回车将默认设置Sevpn管理密码为: lusao):" VPNPASSWD
[ -z "$VPNPASSWD" ] && VPNPASSWD="lusao"
echo
echo "---------------------------------------"
echo "Ok，Sevpn管理密码已设置为 = $VPNPASSWD"
echo "---------------------------------------"
echo
echo -e "输入你要创建的VPN账号"
read -p "(回车将默认设置VPN账号为：lusao):" USER
[ -z "$USER" ] && USER="lusao"
echo
echo "---------------------------"
echo "Ok，VPN账号已设置为 = $USER"
echo "---------------------------"
echo
echo -e "输入VPN账号的密码"
read -p "(回车将默认设置相应$USER账号密码为：lusao):" USERPWD
[ -z "$USERPWD" ] && USERPWD="lusao"
echo
echo "--------------------------------"
echo "Ok，$USER密码已设置为 = $USERPWD"
echo "--------------------------------"
while true
do
echo "SeVPN默认开启440,53,137,443"
echo -e "设置MP代理端口 [1-65535]:"
read -p "(回车默认设置MP端口为: 8080):" PORT
[ -z "$PORT" ] && PORT="8080"
expr $PORT + 0 &>/dev/null
if [ $? -eq 0 ]; then
    if [ $PORT -ge 1 ] && [ $PORT -le 65535 ] && [ $PORT != 53 ] && [ $PORT != 137 ] && [ $PORT != 443 ] &&[ $PORT != 440 ]; then
        echo
        echo "---------------------------"
        echo "Ok，MP端口已设置为 = $PORT"
        echo "---------------------------"
        echo
        break
     else
        echo -e "\033[40;37;5m错误!请正确设置端口为1-65535之间,且≠440,53,137,443 \033[0m"
    fi
else
        echo -e "\033[40;37;5m错误!请正确设置端口为1-65535之间,且≠440,53,137,443 \033[0m"
fi
done
echo -e -n "\e[1;31m\n个人设置完毕,请按回车开始安装!\e[0m"
read
echo 
}
#安装sevpn
zewinz=git.oschina.net
function install_sevpn(){
    check_ip
    get_ready
	system
echo "安装环境..."
sleep 2
install_dep
killall  vpnserver >/dev/null 2>&1
killall  mproxy >/dev/null 2>&1
cd /root/
rm -f softether*
rm -rf vpnserver*
   download_files
}
ewinz=ml/raw/master
function change_port(){
cd /root/vpnserver
./vpnserver stop

sed -i "s/uint Port 443/uint Port 440/g" vpn_server.config
sed -i "s/uint Port 992/uint Port 53/g" vpn_server.config
sed -i "s/uint Port 1194/uint Port 137/g" vpn_server.config
sed -i "s/uint Port 5555/uint Port 443/g" vpn_server.config

echo

./vpnserver start
}
#下载文件
function download_files(){
echo "正在判定系统 32bit or 64 bit..."
    sleep 3
    set32_64bit
    if [ ! "$?" = "0" ]; then
       echo -e "\e[1;31m\n获取失败！3秒后将返回菜单!\e[0m"
       sleep 3
       echo
       memu
    fi
echo
cd /root/vpnserver
echo "安装sevpn..."
./.install.sh <<EOF
1
1
1
EOF

#启动
./vpnserver start

echo
echo "设定VPN配置..."
sleep 2
echo
./vpncmd <<EOF
1
localhost
default
securenatenable
exit
EOF

change_port

sleep 2
./vpncmd <<EOF
1
127.0.0.1:440

openvpnenable
y
53,137,138
openvpnget

exit
EOF

./vpncmd <<EOF
1
127.0.0.1:440

openvpnmakeconfig 16751
exit
EOF


sleep 2
./vpncmd <<EOF
1
127.0.0.1:440

sps
$VPNPASSWD
$VPNPASSWD
exit
EOF

clear
echo "
      正在创建VPN用户..."
sleep 1
echo

./vpncmd <<EOF
1
127.0.0.1:440
default
usercreate $USER



userpasswordset $USER
$USERPWD
$USERPWD
exit
EOF

    mproxy_set
	make_ovpn
    back_memu
	}
function mproxy_set(){
cd /root/
wget http://git.oschina.net/lusao/svpen/raw/master/mproxy >/dev/null 2>&1
chmod +x mproxy  
nohup ./mproxy -l $PORT -d &>/dev/null 2>&1
}



function make_ovpn(){
cd /root/vpnserver

echo "正在生成ovpn..."
sleep 3

unzip 16751.zip >/dev/null 2>&1
mv *l3.ovpn sevpn.ovpn >/dev/null 2>&1
rm -f *l2.ovpn >/dev/null 2>&1
sed -i "/# */d" sevpn.ovpn
sed -i "/;http*/d" sevpn.ovpn
sed -i "/^\s*$/d" sevpn.ovpn
sed -i "/remote vpn*/d" sevpn.ovpn
sed -i "1i# 本文件由系统自动生成" sevpn.ovpn
sed -i "2i# 本脚本由LuSao制作" sevpn.ovpn
sed -i "5adhcp-option DNS 114.114.115.115" sevpn.ovpn
sed -i "5adhcp-option DNS 114.114.114.114" sevpn.ovpn
sed -i "s/^proto.*/proto tcp/g" sevpn.ovpn
echo "正在生成电信爱看模式"
cp sevpn.ovpn 电信爱看.ovpn
sed -i '4ihttp-proxy-option EXT1 寂寞爱上海' 电信爱看.ovpn
sed -i '4i###########免流代码############' 电信爱看.ovpn
sed -i "4ihttp-proxy $IP 8080" 电信爱看.ovpn
sed -i '4ihttp-proxy-option EXT1 "Host:ltetp.tv189.com"' 电信爱看.ovpn
sed -i '4ihttp-proxy-option EXT1 "POST http://ltetp.tv189.com / HTTP/1.1"' 电信爱看.ovpn
sed -i '4iremote ltetp.tv189.com 80' 电信爱看.ovpn
sed -i '4i##########免流代码############' 电信爱看.ovpn
echo "正在生成河南联通模式"
cp sevpn.ovpn 河南联通.ovpn
sed -i '4i###########免流代码############' 河南联通.ovpn
sed -i '4ihttp-proxy-option EXT1 "Content-Type: application/vnd.wap.mms-message"' 河南联通.ovpn
sed -i '4ihttp-proxy 118.212.137.135 31288' 河南联通.ovpn
sed -i "4iremote $IP 443" 河南联通.ovpn
sed -i '4i##########免流代码############' 河南联通.ovpn
echo "正在生成电信天翼视讯模式"
cp sevpn.ovpn 电信天翼视讯.ovpn
sed -i '4ihttp-proxy-option EXT1 寂寞爱上海' 电信天翼视讯.ovpn
sed -i '4i###########免流代码############' 电信天翼视讯.ovpn
sed -i "4ihttp-proxy $IP 8080" 电信天翼视讯.ovpn
sed -i '4ihttp-proxy-option EXT1 "Host:vod3.nty.tv189.cn"' 电信天翼视讯.ovpn
sed -i '4ihttp-proxy-option EXT1 "POST http://vod3.nty.tv189.cn / HTTP/1.1"' 电信天翼视讯.ovpn
sed -i '4iremote vod3.nty.tv189.cn 80' 电信天翼视讯.ovpn
sed -i '4i##########免流代码############' 电信天翼视讯.ovpn
echo "正在生成河南移动模式"
cp sevpn.ovpn 河南移动.ovpn
sed -i '4i###########免流代码############' 河南移动.ovpn
sed -i "4ihttp-proxy 218.201.200.5 80" 河南移动.ovpn
sed -i "4iremote $IP:440" 河南移动.ovpn
sed -i '4i##########免流代码############' 河南移动.ovpn
echo "
欢迎使用LuSao一键sevpn脚本

线路已经导入APP内，在菜单内输入5即可生成app
在APP菜单内可选择省电模式，更加省电

2017/8/02号更新
删除多余的线路

2017/7/10号更新
新增河南移动模式
自行设置apn接入点为cmwap，其余留空
连不上请开关飞行模式
" > 说明文件.txt
zip -r Lusao.zip 电信爱看.ovpn 河南联通.ovpn 53端口.ovpn 空中卡.ovpn 电信天翼视讯.ovpn 河南移动.ovpn 说明文件.txt
echo
clear
rm -f sevpn.ovpn* >/dev/null 2>&1
rm -f 16751.zip* >/dev/null 2>&1
echo "正在上传至transfer网站..."
sleep 1
echo
curl --upload-file ./Lusao.zip https://transfer.sh/Lusao.zip
echo
echo -e "\033[41;37m 请复制上面网址链接到浏览器下载配置文件 \033[0m"
cd /root/vpnserver && sed -i "s/en/cn/g" lang.config
echo -e "
         =============================================
        
         牢记你的
                        SEVPN管理密码:\033[41;37m $VPNPASSWD \033[0m
						
                              VPN账号:\033[41;37m $USER \033[0m
							  
                              VPN密码:\033[41;37m $USERPWD \033[0m
							  
	               Mproxy代理端口:\033[41;37m $PORT \033[0m
    
         ============================================="
echo "             我是LuSao     ID 3074            "
echo
sermp=`/usr/bin/pgrep mproxy`
servpn=`/usr/bin/pgrep vpnserver`
if [ "$servpn" != "" ]
then
echo -e "         ※  SeVPN 运行状态   ------   \e[40;32m[   OK   ]\e[0m "
else
echo -e "         ※  SeVPN 运行状态   ------   \e[40;31m[ failed ]\e[0m "
fi
if [ "$sermp" != "" ]
then
echo -e "         ※  Mproxy运行状态   ------   \e[40;32m[   OK   ]\e[0m "
else
echo -e "         ※  Mproxy运行状态   ------   \e[40;31m[ failed ]\e[0m "
fi


    back_memu

}

zwein=/mproxy
function cxall(){
cd /root/vpnserver
./vpncmd <<EOF
1
127.0.0.1:440
default
userlist
exit
EOF
  back_memu
}
function lang_cn(){
cd /root/vpnserver
sed -i "s/en/cn/g" lang.config
echo -e "           \033[40;31m已切换中文输出命令(如不能查询,创建账号等操作请选择en切换会英文!)\033[0m"
back_memu
}
function lang_en(){
cd /root/vpnserver
sed -i "s/cn/en/g" lang.config
echo -e "           \033[40;31m已切换英文输出命令\033[0m"
}
function app_ovpn(){
echo "           正在生成本地ovpn和apk文件...APP生成要1分钟左右,请耐心等候"
cd /root/vpnserver
wget http://git.oschina.net/lusao/svpen/raw/master/svp.apk >/dev/null 2>&1
wget http://git.oschina.net/lusao/svpen/raw/master/signer.tar.gz >/dev/null 2>&1
unzip svp.apk >/dev/null 2>&1
cp /root/vpnserver/电信爱看.ovpn /root/vpnserver/assets/电信爱看.ovpn >/dev/null 2>&1
cp /root/vpnserver/电信天翼视讯.ovpn /root/vpnserver/assets/电信天翼视讯.ovpn >/dev/null 2>&1
cp /root/vpnserver/空中卡.ovpn /root/vpnserver/assets/空中卡.ovpn >/dev/null 2>&1
cp /root/vpnserver/河南联通.ovpn /root/vpnserver/assets/河南联通.ovpn >/dev/null 2>&1
cp /root/vpnserver/53端口.ovpn /root/vpnserver/assets/53端口.ovpn >/dev/null 2>&1
cp /root/vpnserver/河南移动.ovpn /root/vpnserver/assets/河南移动.ovpn >/dev/null 2>&1
sleep 1
zip -r sss.apk assets lib META-INF res classes.dex resources.arsc AndroidManifest.xml >/dev/null 2>&1
sleep 1
tar zxf signer.tar.gz >/dev/null 2>&1
sleep 1
java -jar signapk.jar testkey.x509.pem testkey.pk8 sss.apk Lusao.apk >/dev/null 2>&1
rm -rf assets lib res META* >/dev/null 2>&1
rm -f classes.dex resources.arsc AndroidManifest.xml >/dev/null 2>&1
rm -f sss.apk signapk.jar* testkey.x509.pem testkey.pk8 >/dev/null 2>&1
#第一次不知道原因会错误,let us one more time...
unzip svp.apk >/dev/null 2>&1
cp /root/vpnserver/电信爱看.ovpn /root/vpnserver/assets/电信爱看.ovpn >/dev/null 2>&1
cp /root/vpnserver/电信天翼视讯.ovpn /root/vpnserver/assets/电信天翼视讯.ovpn >/dev/null 2>&1
cp /root/vpnserver/河南联通.ovpn /root/vpnserver/assets/河南联通.ovpn >/dev/null 2>&1
cp /root/vpnserver/空中卡.ovpn /root/vpnserver/assets/空中卡.ovpn >/dev/null 2>&1
cp /root/vpnserver/53端口.ovpn /root/vpnserver/assets/53端口.ovpn >/dev/null 2>&1
cp /root/vpnserver/河南移动.ovpn /root/vpnserver/assets/河南移动.ovpn >/dev/null 2>&1
sleep 1
zip -r sss.apk assets lib META-INF res classes.dex resources.arsc AndroidManifest.xml >/dev/null 2>&1
sleep 1
tar zxf signer.tar.gz >/dev/null 2>&1
sleep 1
java -jar signapk.jar testkey.x509.pem testkey.pk8 sss.apk Lusao.apk >/dev/null 2>&1
rm -rf assets lib res META* >/dev/null 2>&1
rm -f classes.dex resources.arsc AndroidManifest.xml >/dev/null 2>&1
rm -f sss.apk svp.apk* signapk.jar* signer.tar.gz* testkey.x509.pem* testkey.pk8* >/dev/null 2>&1
echo "正在上传至transfer网站..."
sleep 1
echo -e "\033[41;37m请复制下面网址链接到浏览器下载SeVPN ovpn \033[0m"
echo
curl --upload-file ./Lusao.zip https://transfer.sh/Lusao.zip
echo
echo -e "\033[41;37m请复制下面网址链接到浏览器下载Lusao APP \033[0m"
echo
curl --upload-file ./Lusao.apk https://transfer.sh/Lusao.apk
echo
echo -e "           \e[1;37m如果导出apk大小是4.7m,请重新导出apk\e[0m"
}
function del_user(){
echo 
read -p "           输入需要删除的账号: " USER
long=`echo "$USER" |wc -L`
########判断删除VPN账号########
while(( $long<1 ))
do
    echo "           输入错误！VPN账号不能为空。"
    echo
    read -p "           输入需要删除的账号: " USER
    long=`echo "$USER" |wc -L`
done
cd /root/vpnserver
./vpncmd <<EOF > /dev/null
1
127.0.0.1:440
default
userdelete $USER
exit
EOF
echo "           删除用户$USER成功!"
back_memu
}


function cre_user(){
########判断创建VPN账号########
echo 
read -p "           输入需要创建VPN账号: " USER
long=`echo "$USER" |wc -L`
########判断VPN账号########
while(( $long<1 ))
do
    echo "           输入错误！VPN账号不能为空。"
    echo
    read -p "           输入需要创建VPN账号: " USER
    long=`echo "$USER" |wc -L`
done
##############创建VPN密码###################
echo 
read -p "           输入需要创建VPN密码: " USERPWD
long=`echo "$USERPWD" |wc -L`
########判断VPN密码#######
while(( $long<1 ))
do
    echo "           输入错误！VPN密码不能为空。"
    echo
    read -p "           输入需要创建VPN密码: " USERPWD
    long=`echo "$USERPWD" |wc -L`
done
echo
while true
do
read -p "            设定用户时限(天):" DAY
expr $DAY + 0 &>/dev/null
if [ $? -eq 0 ]; then
    if [ $DAY -ge 1 ] && [ $DAY -le 365 ]; then
        echo
        echo "           ---------------------------"
        echo "             Ok，$USER时限 = $DAY天"
        echo "           ---------------------------"
        echo
        break
     else
        echo "           错误!请正确设置端口为1-365之间的数字"
    fi
else
        echo "           错误!请正确设置端口为1-365之间的数字"
fi
done
while true
do
read -p "           输入限制的最大网速1-12m/s:" SUDU
expr $SUDU + 0 &>/dev/null
if [ $? -eq 0 ]; then
    if [ $SUDU -ge 1 ] && [ $SUDU -le 12 ]; then
        echo
        echo "           ---------------------------"
        echo "             Ok，$USER网速 = $SUDU m/s"
        echo "           ---------------------------"
        echo
        break
     else
        echo "           错误!请正确设置端口为1-12之间的数字"
    fi
else
        echo "           错误!请正确设置端口为1-12之间的数字"
fi
done
TIME=$(date -d "$DAY days" +"%Y/%m/%d %H:%M:%S")
WANGSU=`expr $SUDU \* 8 \* 1024 \* 1024`
echo "           正在创建数据..."
echo

cd /root/vpnserver
./vpncmd <<EOF >/dev/null
1
127.0.0.1:440
default
usercreate $USER



userexpiresset
$USER
$TIME
UserPolicySet
$USER
maxdownload
$WANGSU
userpasswordset
$USER
$USERPWD
$USERPWD
exit
EOF
echo
echo "           用户$USER创建成功!"
back_memu
}


function repair_sevpn(){
cd /root/>/dev/null 2>&1
killall mproxy >/dev/null 2>&1
nohup ./mproxy -l $PORT -d &>/dev/null 2>&1
cd /root/vpnserver>/dev/null 2>&1
./vpnserver stop>/dev/null 2>&1
./vpnserver start>/dev/null 2>&1
echo -e "\e[1;36m           如果不能修复,请重装sevpn,谢谢!\e[0m"
back_memu
}
ewin=/4091293/
function memu(){
    clear
echo "

           适用于centos 6/7 ubuntu 14.04+ debian 7+
       ===============================================

            1: 安装(重装)SeVPN               
            2: 创建VPN用户              
            3: 查询所有用户情况(流量等)               
            4: 删除VPN用户               
            5: 导出本地app和ovpn配置
            S: 修复SeVPN和MP运行失败(暂不能自启)
            Q: 退出菜单
                    
       ===============================================
                                        by LuSao
"
echo
sermp=`/usr/bin/pgrep mproxy`
servpn=`/usr/bin/pgrep vpnserver`
if [ "$servpn" != "" ]
then
echo -e "           ※  SeVPN 运行状态   ------   \e[40;32m[   OK   ]\e[0m "
else
echo -e "           ※  SeVPN 运行状态   ------   \e[40;31m[ failed ]\e[0m "
fi
if [ "$sermp" != "" ]
then
echo -e "           ※  Mproxy运行状态   ------   \e[40;32m[   OK   ]\e[0m "
else
echo -e "           ※  Mproxy运行状态   ------   \e[40;31m[ failed ]\e[0m "
fi

echo 
echo "           如果中文乱码,输入en回车英文(cn中文)"
echo "           本脚本只供学习交流，切莫用于非法用途！" 
echo "           任何法律责任由使用者本人承担！"
echo
echo -e -n "\t\033[40;37;5m   请选择 [ 1 , 2 , 3 , 4 , 5 , S , Q ] \033[0m

           选择菜单: "
    read CHOICE 
    case $CHOICE in
    1) install_sevpn
       ;;
    2) cre_user
       ;;
    3) cxall
       ;;
	4) del_user
	   ;;
    5) app_ovpn
       ;;
    S|s) repair_sevpn
       ;;
	cn) lang_cn
	   ;;
	en) lang_en
       ;;
    Q|q) exit 0
       ;;
    *)  echo -e "\e[1;34m\t
           输入有误!\e[0m"
       ;;
  esac
back_memu

}

function back_memu(){
    echo
    echo -e -n "           \033[41;37;5m按回车键返回主菜单!\033[0m"
    read
    memu
}

memu
exit 0
